package com.example.administrator.nurse.widget;

import android.app.Activity;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.example.administrator.nurse.R;

/**
 *  Created by 林冠宏 on 2016/5/25.
 *  公共的 搜索 header 配置类
 *
 */

public class SearchInput  {

    private Activity activity;
    private View view;

    public TextView sort,type;
    public LoginInputEdit searchInput;

    private int typeMenuId = -1;
    private int sortMenuId = -1;

    public SearchInput(View view){
        this.view = view;
    }

    public SearchInput(Activity activity,View view){
        this.activity = activity;
        this.view    = view;
    }

    public SearchInput withTypeMenu(int typeMenuId){
        this.typeMenuId = typeMenuId;
        return this;
    }

    public SearchInput withSortMenu(int sortMenuId){
        this.sortMenuId = sortMenuId;
        return this;
    }

    public SearchInput init(){
        /** 分类点击 */
        type = (TextView) view.findViewById(R.id.type);
        type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu2 = new PopupMenu(activity, type);
                popupMenu2.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        if(OnMenuItemClickListener!=null){
                            OnMenuItemClickListener.menuTypeItemClick(item);
                        }
                        return true;
                    }
                });
                if(typeMenuId!=-1){
                    activity.getMenuInflater().inflate(typeMenuId, popupMenu2.getMenu());
                    popupMenu2.show();
                }
            }
        });
        /** 排序点击 */
        sort = (TextView) view.findViewById(R.id.count);
        sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu2 = new PopupMenu(activity, sort);
                popupMenu2.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        if (OnMenuItemClickListener != null) {
                            OnMenuItemClickListener.menuSortItemClick(item);
                        }
                        return true; // 返回 true 点击关闭
                    }
                });
                if(sortMenuId!=-1){
                    activity.getMenuInflater().inflate(sortMenuId, popupMenu2.getMenu());
                    popupMenu2.show();
                }
            }
        });

        searchInput = (LoginInputEdit) view.findViewById(R.id.searchInput);
        searchInput.numberLimit = 1;
        /** 搜索 */
        view.findViewById(R.id.search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchInput.isOk("搜索关键字不能少于1个")) {
                    if(OnSearchClickListener!=null){
                        OnSearchClickListener.searchClick(searchInput);
                    }
                }
            }
        });
        return this;
    }

    private OnMenuItemClickListener OnMenuItemClickListener;
    public SearchInput withMenuItemClickListener(OnMenuItemClickListener OnMenuItemClickListener){
        this.OnMenuItemClickListener = OnMenuItemClickListener;
        return this;
    }

    private OnSearchClickListener OnSearchClickListener;
    public SearchInput withSearchClickListener(OnSearchClickListener OnSearchClickListener){
        this.OnSearchClickListener = OnSearchClickListener;
        return this;
    }

    public interface OnMenuItemClickListener{
        public void menuTypeItemClick(MenuItem item);
        public void menuSortItemClick(MenuItem item);
    }

    public interface OnSearchClickListener{
        public void searchClick(LoginInputEdit searchInput);
    }

}




